cmake_minimum_required( VERSION 2.8 )
project(on_board_system)

set( CMAKE_CXX_FLAGS "-std=c++14" )

set(CMAKE_C_FLAGS "-pthread -std=gnu99")
set(CMAKE_EXE_LINKER_FLAGS "-pthread")
set(CMAKE_C_COMPILER "gcc")
set(CMAKE_CXX_COMPILER "g++")

execute_process(COMMAND uname -m
        OUTPUT_VARIABLE DEVICE_SYSTEM_ID)
message(WARNING " device system id: ${DEVICE_SYSTEM_ID}")

if (DEVICE_SYSTEM_ID MATCHES x86_64)
    set(TOOLCHAIN_NAME x86_64-linux-gnu-gcc)
    add_definitions(-DPLATFORM_ARCH_x86_64=1)
elseif (DEVICE_SYSTEM_ID MATCHES aarch64)
    set(TOOLCHAIN_NAME aarch64-linux-gnu-gcc)
    add_definitions(-DPLATFORM_ARCH_aarch64=1)
else ()
    message(FATAL_ERROR "FATAL: Please confirm your platform.")
endif ()

set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/src/3rdparty)

find_package( OpenCV 3 REQUIRED )
add_definitions(-DOPEN_CV_VERSION_3)

find_package(FFMPEG REQUIRED)
add_definitions(-DFFMPEG_INSTALLED)

find_package(LIBUSB REQUIRED)
add_definitions(-DLIBUSB_INSTALLED)

find_package(jsoncpp REQUIRED)

# for ekf type , here use Pdot = F * P * F' + Q
add_definitions(-DEKF_UPDATE_DISCRETE)

include_directories( 
	psdk_lib/include
	src
	src/liveView
	src/pid
	src/siftApi
	src/assistLandMode
	src/circularBuf
	src/linkProtocol
	src/ekf
	src/ukf
	src/stereoApi
	src/elas
	src/opticalFlow
	src/base64
	src/utils
	src/cnnCopterDetection
	src/assistAvoidMode
  	${OpenCV_INCLUDE_DIRS}
  	${FFMPEG_INCLUDE_DIR}
  	${JSON_INC_PATH}
  )

if (DEVICE_SYSTEM_ID MATCHES x86_64)
 	link_directories( ${CMAKE_CURRENT_LIST_DIR}/psdk_lib/lib/x86_64-linux-gnu-gcc)
 	link_libraries( ${CMAKE_CURRENT_LIST_DIR}/psdk_lib/lib/x86_64-linux-gnu-gcc/libpayloadsdk.a -lstdc++)
elseif (DEVICE_SYSTEM_ID MATCHES aarch64)
	#sunrise X3 / orange pi
	link_directories( ${CMAKE_CURRENT_LIST_DIR}/psdk_lib/lib/aarch64-linux-gnu-gcc)
	link_libraries( ${CMAKE_CURRENT_LIST_DIR}/psdk_lib/lib/aarch64-linux-gnu-gcc/libpayloadsdk.a -lstdc++)
 	#C4 Pi
	#link_directories( ${CMAKE_CURRENT_LIST_DIR}/psdk_lib/lib/arm-linux-gnueabihf-gcc)
	#link_libraries( ${CMAKE_CURRENT_LIST_DIR}/psdk_lib/lib/arm-linux-gnueabihf-gcc/libpayloadsdk.a -lstdc++)
else ()
    message(FATAL_ERROR "FATAL: Please confirm your platform.")
endif ()


add_library(pid STATIC src/pid/pid.cpp)
add_library(ac_pid STATIC src/pid/ac_pid.cpp)
add_library(circular_buf STATIC src/circularBuf/circular_buf.cpp)
add_library(ekf STATIC src/ekf/ekf.cpp  src/ekf/matrix.cpp)
add_library(ukf STATIC src/ukf/ukf.cpp src/ukf/linalg.cpp)
add_library(base64 STATIC src/base64/encode.c src/base64/decode.c src/base64/buffer.c)

add_executable(on_board_system  	src/main.cpp
									src/dji_flight_handler.cpp
									src/application.cpp
									src/hal/hal_network.c
									src/hal/hal_uart.c
									src/hal/hal_usb_bulk.c
									src/osal/osal.c
									src/osal/osal_fs.c
									src/osal/osal_socket.c
									src/liveView/dji_camera_image_handler.cpp
									src/liveView/dji_camera_stream_decoder.cpp
									src/assistLandMode/assist_land.cpp
									src/siftApi/sift_api.cpp
									src/linkProtocol/link_protocol.cpp
									src/utils/util_md5.c
									src/utils/util_misc.c
									src/assistAvoidMode/assist_avoid.cpp)


target_link_libraries( on_board_system  ${OpenCV_LIBS}
										${FFMPEG_LIBRARIES}
										pid
										ac_pid
										circular_buf
										libpayloadsdk.a
										usb-1.0
										lzma
										swresample
										m
										z
										jsoncpp_lib
										ekf
										ukf
										base64)

#sift test
add_executable(sift_test  src/sift_test.cpp
						  src/siftApi/sift_api.cpp)
target_link_libraries(sift_test ${OpenCV_LIBS})


#stereo test
add_executable(stereo_test 	src/stereo_test.cpp
							src/stereoApi/stereo_api.cpp)
target_link_libraries(stereo_test ${OpenCV_LIBS})

#elas test
if (DEVICE_SYSTEM_ID MATCHES x86_64)
add_executable(elas_test src/elas_test.cpp
						 src/elas/elas.cpp
						 src/elas/descriptor.cpp
						 src/elas/filter.cpp
						 src/elas/triangle.cpp
						 src/elas/matrix.cpp)
target_link_libraries(elas_test ${OpenCV_LIBS})
endif ()

#optical flow test
add_executable(optical_flow_test 	src/optical_flow_test.cpp
							src/opticalFlow/optical_flow.cpp)
target_link_libraries(optical_flow_test ${OpenCV_LIBS})


#base64 test
add_executable(base64_test	src/base64_test.c
							src/base64/encode.c
							src/base64/decode.c
							src/base64/buffer.c)

#cnn copter detection test
add_executable(cnn_copter_detection_test src/cnn_copter_detection_test.cpp
										 src/cnnCopterDetection/cnn.cpp
										 src/cnnCopterDetection/fc.cpp
										 src/cnnCopterDetection/pool.cpp
										 src/cnnCopterDetection/relu.cpp
										 src/cnnCopterDetection/drop_out.cpp)